package com.young.fragments.demo.lazy_show_demo.fms

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.young.fragments.databinding.ActivityLazyShowBinding
import com.young.lib_fragments.fms.BaseLazyFragment

/**
 * @author huangzehui
 * @date 2025/1/24 13:00 周五
 * @description 懒加载 Fragment 4
 */
class LazyShowFragment4 : BaseLazyFragment(), View.OnClickListener {

    private lateinit var mBinding: ActivityLazyShowBinding

    private lateinit var mFragmentStateAdapter: FragmentStateAdapter

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mBinding = ActivityLazyShowBinding.inflate(inflater, container, false)
        return mBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

    }

    override fun onResume() {
        super.onResume()
        Log.e("young_test909090909", "onResume = ${this}")
    }

    override fun onFirstSupportVisible() {
        super.onFirstSupportVisible()

        mFragmentStateAdapter = object : FragmentStateAdapter(this) {

            override fun getItemCount(): Int {
                return 3
            }

            override fun createFragment(position: Int): Fragment {
                return when (position) {
                    0 -> {
                        LazyShowFragment1()
                    }

                    1 -> {
                        LazyShowFragment2()
                    }

                    2 -> {
                        LazyShowFragment3()
                    }


                    else -> {
                        throw Exception("no fragment")
                    }
                }
            }

        }

        initViewPager()
        initClick()
    }


    private fun initViewPager() {
        mBinding.vpContainer.offscreenPageLimit = 3
        mBinding.vpContainer.adapter = mFragmentStateAdapter
    }

    private fun initClick() {
        mBinding.btnPage1.setOnClickListener(this)
        mBinding.btnPage2.setOnClickListener(this)
        mBinding.btnPage3.setOnClickListener(this)

        mBinding.btnPage4.visibility = View.GONE
    }

    override fun onClick(v: View?) {
        when (v) {
            mBinding.btnPage1 -> {
                mBinding.vpContainer.currentItem = 0
            }

            mBinding.btnPage2 -> {
                mBinding.vpContainer.currentItem = 1
            }

            mBinding.btnPage3 -> {
                mBinding.vpContainer.currentItem = 2
            }

            mBinding.btnPage4 -> {
                mBinding.vpContainer.currentItem = 3
            }

            else -> {

            }
        }
    }

}